package com.food.admin.controller;


import com.food.admin.entity.ProductsType;
import com.food.admin.exception.MyParameterException;
import com.food.admin.exception.foodAdminException;
import com.food.admin.form.ProductsTypeAddForm;
import com.food.admin.form.ProductsTypeSearchForm;
import com.food.admin.form.ProductsTypeUpdateForm;
import com.food.admin.form.ProductsTypeUpdateStateForm;
import com.food.admin.model.Result;
import com.food.admin.service.ProductsTypeService;
import com.food.admin.util.MyPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 帅得一
 * @since 2023年06月13日
 */
@RestController
@RequestMapping("/productsType")
@Api(tags = "餐品类型接口")
public class ProductsTypeController {
    /**
     * 商品类别接口  会涉及到类别的增删改查  id  typename  state  0 删除  1 正常  2 下架
     */
    @Autowired
    private ProductsTypeService productsTypeService;
    @ApiOperation("新增类型接口")
    @PostMapping("/addProductsType")
    @Transactional
    public Result addProductsType(@RequestBody @Valid ProductsTypeAddForm productsTypeAddForm,BindingResult br) throws Exception {
        if (br.hasErrors()) {
            //获取所有参数校验过程中出现的错误
           throw new MyParameterException(500,"参数校验失败");
        }
        productsTypeService.addProductsType(productsTypeAddForm);
        return Result.success("200","新增餐品类型成功！",null);
    }
    @PostMapping("/updateProductsTypeName")
    @ApiOperation("修改餐品类型名称")
    @Transactional
    public Result updateProductsTypeName(@RequestBody @Valid ProductsTypeUpdateForm productsTypeUpdateForm,BindingResult br){
        if(br.hasErrors()){
            //获取所有参数校验过程中出现的错误
            List<ObjectError> allErrors = br.getAllErrors();
            for (ObjectError allError : allErrors) {
                //获取提示信息并打印
                System.out.println(allError.getDefaultMessage());
            }
            throw new MyParameterException(500,"参数校验失败");
        }
        productsTypeService.updateProductsTypeName(productsTypeUpdateForm);
        return Result.success("200","修改餐品类型名称成功！",null);
    }
    @PostMapping("/updateProductsTypeState")
    @ApiOperation("修改餐品类型状态")
    @Transactional
    public Result updateProductsTypeState(@RequestBody @Valid ProductsTypeUpdateStateForm productsTypeUpdateStateForm,BindingResult br){
        if (br.hasErrors()){
            List<ObjectError> allErrors = br.getAllErrors();
            for (ObjectError allError : allErrors) {
                //获取提示信息并打印
                System.out.println(allError.getDefaultMessage());
            }
            throw new MyParameterException(500,"参数校验失败");
        }
        productsTypeService.updateProductsTypeState(productsTypeUpdateStateForm);

        return Result.success("200","修改餐品类型状态成功！",null);
    }
    @GetMapping("/searchAllProductsType")
    @ApiOperation("查询所有餐品类型")
    public Result searchAllProductsType(){
        List<ProductsType> list=productsTypeService.searchAllProductsType();
        return Result.success("200","查询所有餐品类型成功！",list);
    }
    @GetMapping("/searchProducts")
    @ApiOperation("条件查询餐品类型")
    public Result searchProductsType(ProductsTypeSearchForm productsTypeSearchForm){
        System.out.println(productsTypeSearchForm);
        MyPage<ProductsType> page=  productsTypeService.searchProductsType(productsTypeSearchForm);
        return Result.success("200","条件查询类型成功！",page);
    }

}

